import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import users from '../components/users/users.vue'
import rights from '../components/powers/rights.vue'
import roles from '../components/powers/roles.vue'
import categories from '../components/goods/categories.vue'
import params from '../components/goods/params.vue'
import goods from '../components/goods/goods.vue'
import add from '../components/goods/add.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    { path: '/home', component: home, children: [{ path: '/home', redirect: '/welcome' }, { path: '/welcome', component: welcome }, { path: '/users', component: users }, { path: '/rights', component: rights }, { path: '/roles', component: roles }, { path: '/categories', component: categories }, { path: '/params', component: params }, { path: '/goods', component: goods }, { path: '/goods/add', component: add }] }
  ]
})
router.beforeEach((to, from, next) => { // 导航守卫
  if (to.path === '/login') return next() // 访问登录直接放行
  if (!sessionStorage.getItem('token')) return next('/login') // 判断是否有token
  next()
})

export default router
